package com.test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;

public class MyTankGame6 extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;
	MyPanel mp = null;
	// definit un panel pour le debut
	StartPanel msp = null;
	// le menu
	JMenuBar jmb = null;
	// options
	JMenu jm1 = null;
	JMenu jm2 = null;
	JMenu jm3 = null;

	JMenuItem jmi1 = null;
	JMenuItem jmi2 = null;
	JMenuItem jmi3 = null;
	JMenuItem jmi4 = null;
	JRadioButtonMenuItem jmi5 = null;
	JRadioButtonMenuItem jmi6 = null;
	JRadioButtonMenuItem jmi7 = null;
	JMenuItem jmi8 = null;

	// Label pour les Jmenuitems
	String jmil1 = null;
	String jmil2 = null;
	String jmil3 = null;
	String jmil4 = null;
	String jmil5 = null;
	String jmil6 = null;
	String jmil7 = null;
	String jmil8 = null;
	String jmil9 = null;
	String jmil10 = null;
	String jmil11 = null;

	private static int language = 0;// (0--fr,1--en,2--zh)

	private static MyTankGame6 tank;

	public static void main(String[] args) {
		setTank(new MyTankGame6());

	}

	public MyTankGame6() {

		verifLang();
		this.setSize(600, 425);
		this.setJMenuBar(jmb);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		this.setLocation(300, 150);
		//this.setAlwaysOnTop(true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getActionCommand().equals("nvpartie")) {
			if (mp != null) {
				this.remove(mp);
				mp.clear();
				this.removeKeyListener(mp);
				mp = null;

			}
			if (msp != null) {
				this.remove(msp);
				msp.getJf().removeKeyListener(msp);
				msp = null;
			}
			System.gc();
			msp = new StartPanel();
			Thread t = new Thread(msp);
			t.start();
			msp.setJf(this);
			this.add(msp);
			this.addKeyListener(msp);

		} else if (arg0.getActionCommand().equals("quitter")) {
			System.exit(0);
		} else if (arg0.getActionCommand().equals("sauvegarder")) {

			// sauvegarder la location des chars
			Recorder rec = new Recorder();
			rec.setEts(this.getMp().ets);
			rec.keepRecord();
			// quitter l'app
			System.exit(0);
		} else if (arg0.getActionCommand().equals("continuer")) {

			MyPanel mp = new MyPanel(1);
			this.add(mp);
			this.addKeyListener(mp);
			this.setMp(mp);
			this.setVisible(true);
			mp.setJf(this);

		} else if (arg0.getActionCommand().equals("fr")) {
			setLanguage(0);
			verifLang();
			this.setJMenuBar(jmb);
			this.invalidate();
			this.validate();
			this.repaint();
			
		} else if (arg0.getActionCommand().equals("en")) {
			setLanguage(1);
			verifLang();
			this.setJMenuBar(jmb);
			this.invalidate();
			this.validate();
			this.repaint();
			

		} else if (arg0.getActionCommand().equals("zh")) {
			setLanguage(2);
			verifLang();
			this.setJMenuBar(jmb);
			this.invalidate();
			this.validate();
			this.repaint();
			

		}else if(arg0.getActionCommand().equals("help")) {
			JDialog jd = new JDialog();
			jd.setSize(200,100);
			jd.setLocationRelativeTo(this);
			jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			jd.setVisible(true);
		}
		
		this.setVisible(true);

	}

	public void verifLang() {
		if (getLanguage() == 0) {
			jmil1 = "Jeu(J)";
			jmil2 = "Langue(L)";
			jmil3 = "Nouvelle Partie(N)";
			jmil4 = "Quitter(Q)";
			jmil5 = "Sauvegarder(S) et Quitter";
			jmil6 = "Continuer(C) la dernière partie";
			jmil7 = "Français(F)";
			jmil8 = "Anglais(A)";
			jmil9 = "Chinois(C)";
			jmil10 = "?";
			jmil11 = "Aide(I)";

		} else if (getLanguage() == 1) {
			jmil1 = "Game(G)";
			jmil2 = "Language(L)";
			jmil3 = "New Game(N)";
			jmil4 = "Exit(E)";
			jmil5 = "Save and Quit(S)";
			jmil6 = "Load(L)";
			jmil7 = "French(F)";
			jmil8 = "English(E)";
			jmil9 = "Chinese(C)";
			jmil10 = "?";
			jmil11 = "Help(H)";

		} else if (getLanguage() == 2) {
			jmil1 = "选项(G)";
			jmil2 = "语言(L)";
			jmil3 = "新游戏(N)";
			jmil4 = "退出(E)";
			jmil5 = "保存并退出(S)";
			jmil6 = "加载存档(L)";
			jmil7 = "法语(F)";
			jmil8 = "英语(E)";
			jmil9 = "中文(C)";
			jmil10 = "?";
			jmil11 = "帮助(H)";

		}

		// on construit le menu bar
		jmb = new JMenuBar();

		// le menu

		jm1 = new JMenu(jmil1);

		jm2 = new JMenu(jmil2);

		jm3 = new JMenu(jmil10);

		// les menu items pour menu jeu
		jmi1 = new JMenuItem(jmil3);

		jmi2 = new JMenuItem(jmil4);

		jmi3 = new JMenuItem(jmil5);

		jmi4 = new JMenuItem(jmil6);

		// les menu items pour menu language
		if(getLanguage()==0) {
			jmi5 = new JRadioButtonMenuItem(jmil7,true);

			jmi6 = new JRadioButtonMenuItem(jmil8);

			jmi7 = new JRadioButtonMenuItem(jmil9);
		}else if(getLanguage()==1) {
			jmi5 = new JRadioButtonMenuItem(jmil7);

			jmi6 = new JRadioButtonMenuItem(jmil8,true);

			jmi7 = new JRadioButtonMenuItem(jmil9);
		}else if(getLanguage()==2) {
			jmi5 = new JRadioButtonMenuItem(jmil7);

			jmi6 = new JRadioButtonMenuItem(jmil8);

			jmi7 = new JRadioButtonMenuItem(jmil9,true);
			
		}
		ButtonGroup bg = new ButtonGroup();
		bg.add(jmi5);
		bg.add(jmi6);
		bg.add(jmi7);

		// les menu items pour menu aide
		jmi8 = new JMenuItem(jmil11);

		// raccourcis
		if (getLanguage() == 0) {
			jm1.setMnemonic('J');
			jm2.setMnemonic('L');
			jm3.setMnemonic('H');
			jmi1.setMnemonic('N');
			jmi2.setMnemonic('Q');
			jmi3.setMnemonic('S');
			jmi4.setMnemonic('C');
			jmi5.setMnemonic('F');
			jmi6.setMnemonic('A');
			jmi7.setMnemonic('C');
			jmi8.setMnemonic('I');
		} else if (getLanguage() == 1 || getLanguage() == 2) {
			jm1.setMnemonic('G');
			jm2.setMnemonic('L');
			jm3.setMnemonic('H');
			jmi1.setMnemonic('N');
			jmi2.setMnemonic('E');
			jmi3.setMnemonic('S');
			jmi4.setMnemonic('l');
			jmi5.setMnemonic('F');
			jmi6.setMnemonic('E');
			jmi7.setMnemonic('C');
			jmi8.setMnemonic('H');
		}

		// action pour jmi1
		jmi1.addActionListener(this);
		jmi1.setActionCommand("nvpartie");
		// action pour jmi2
		jmi2.addActionListener(this);
		jmi2.setActionCommand("quitter");
		// action pour jmi3
		jmi3.addActionListener(this);
		jmi3.setActionCommand("sauvegarder");
		jmi3.setEnabled(false);
		// action pour jmi4
		jmi4.addActionListener(this);
		jmi4.setActionCommand("continuer");
		Recorder rec = new Recorder();
		if (!rec.isExist()) {
			jmi4.setEnabled(false);
		}
		// action pour jmi5
		jmi5.addActionListener(this);
		jmi5.setActionCommand("fr");
		// action pour jmi6
		jmi6.addActionListener(this);
		jmi6.setActionCommand("en");
		// action pour jmi7
		jmi7.addActionListener(this);
		jmi7.setActionCommand("zh");
		// action pour jmi8
		jmi8.addActionListener(this);
		jmi8.setActionCommand("help");

		// ajout dans menu1
		jm1.add(jmi1);
		jm1.add(jmi3);
		jm1.add(jmi4);
		jm1.add(jmi2);
		// ajout dans menu2
		jm2.add(jmi5);
		jm2.add(jmi6);
		jm2.add(jmi7);
		// ajout dans menu3
		jm3.add(jmi8);

		jmb.add(jm1);
		jmb.add(jm2);
		jmb.add(jm3);
	}

	public MyPanel getMp() {
		return mp;
	}

	public void setMp(MyPanel mp) {
		this.mp = mp;
	}

	public static MyTankGame6 getTank() {
		return tank;
	}

	public static void setTank(MyTankGame6 tank) {
		MyTankGame6.tank = tank;
	}

	public int getLanguage() {
		return language;
	}

	public static void setLanguage(int language) {
		MyTankGame6.language = language;
	}

}
